﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Dimeng.ArdisPostProcessor.HomagLC4.Ardis;
using ReadR41;
using Dimeng.ArdisPostProcessor.HomagLC4Adsn;

namespace WpfApplication1
{
    public class R41Creator
    {
        Bootstrap bootstrap;
        public R41Creator(Bootstrap _bootstrap)
        {
            bootstrap = _bootstrap;
        }

        /// <summary>
        /// 生成cop
        /// </summary>
        public void CreateCop(string directory, string copName)
        {
            string stkpath = Path.Combine(directory, copName + ".stk");
            string stdpath = Path.Combine(directory, copName + ".std");
            string mchpath = Path.Combine(directory, copName + ".mch");
            string r41path = Path.Combine(directory, copName + ".r41");
            string copPath = Path.Combine(directory, copName + ".COP");
            Encoding coding = Encoding.GetEncoding(936);

            #region step1.生成stk板件清单
            using (StreamWriter stk = new StreamWriter(stkpath, false, coding))
            {
                int pi = 0;
                var r2 = bootstrap.GetResult2();
                r2.Patterns.ForEach(it => patternToStk(stk, it, ref pi, "Pre" + bootstrap.Result2Comments[r2.Patterns.IndexOf(it)]));
            }
            #endregion

            #region step2.生成std材料清单
            using (StreamWriter std = new StreamWriter(stdpath, false, coding))
            {

                std.WriteLine("[ISTDALG]");
                std.WriteLine($"ALGMACH={copName}");
                std.WriteLine("[ISTDALG]");
                int mi = 0;
                var originData = bootstrap.GetOrigin();
                originData.Sheets.ForEach(it => sheetToStd(std, it, originData.Materials, ref mi));
            }
            #endregion

            #region step3.生成mch文件
            using (StreamWriter mch = new StreamWriter(mchpath, false, coding))
            {
                mch.WriteLine("[MACHTB]");
                mch.WriteLine($"OMSCHR={Properties.Settings.Default.MachineName}");
                mch.WriteLine($"SNDD1L={Properties.Settings.Default.BladeThick}");
                mch.WriteLine($"MAXNIV={Properties.Settings.Default.MaxLevel}");
                mch.WriteLine($"STPH={Properties.Settings.Default.BookHeight}");
                mch.WriteLine("[MACHTB]");
            }
            #endregion

            #region step4.生成r41文件
            using (StreamWriter r41 = new StreamWriter(r41path, false, coding))
            {
                r41.WriteLine("[STKB-1]");
                r41.WriteLine($"NAAM={copName}");
                r41.WriteLine("[STKB-1]");

                r41.WriteLine("[STDB-1]");
                r41.WriteLine($"NAAM={copName}");
                r41.WriteLine("[STDB-1]");
                r41.WriteLine("[OPTPAR]");
                r41.WriteLine("PRI=2");//余料优先
                //r41.WriteLine($"AlgStack={(int)projSet.AlgorithmFotStocking}");
                r41.WriteLine("[OPTPAR]");

            }
            #endregion

            #region step5.压缩文件为cop
            string[] copfiles = { stkpath, stdpath, mchpath, r41path };
            SystemIOHelper.Compress(copfiles, copPath);
            SystemIOHelper.DeleteFiles(copfiles);
            #endregion
            SystemIOHelper.OpenFolderAndSelectFile(copPath);
        }

        private void sheetToStd(StreamWriter sw, Sheet sheet, List<Material> materials, ref int index)
        {
            index++;
            var mat = materials.Find(it => sheet.SheetMatNo == it.MaterialNo);
            if (mat == null)
                throw new NullReferenceException("未找到对应材料");

            sw.WriteLine($"[ISTD-{index}]");
            sw.WriteLine($"MATERIAAL={mat.MaterialName}");
            sw.WriteLine($"BREEDTE={sheet.SheetW}");
            sw.WriteLine($"LENGTE={sheet.SheetL}");
            sw.WriteLine($"DIKTE={sheet.SheetThick}");
            sw.WriteLine($"RICHTING={0}");//暂时用无纹理，找不到字段
            sw.WriteLine($"AANT={9999}");
            //材料使用方式
            if (sheet.SheetL > Properties.Settings.Default.NormalLength || sheet.SheetW > Properties.Settings.Default.NormalWidth)
                sw.WriteLine($"NOLIMIT={0}");

            else
                sw.WriteLine($"NOLIMIT={2}");
            //不修边
            sw.WriteLine($"MINBRDBOVEN={0}");
            sw.WriteLine($"MINBRDRECHTS={0}");
            sw.WriteLine($"MINBRDONDER={0}");
            sw.WriteLine($"MINBRDLINKS={0}");
            sw.WriteLine($"[ISTD-{index}]");
        }

        private void patternToStk(StreamWriter sw, Pattern pattern, ref int index, string name)
        {
            var part = bootstrap.Result2Parts[pattern.PatternNumber];
            index++;
            sw.WriteLine($"[ISTK-{index}]");
            sw.WriteLine($"MAT={pattern.PatternMaterial}");
            sw.WriteLine($"L={pattern.PatternL}");
            sw.WriteLine($"B={pattern.PatternW}");
            sw.WriteLine($"A={pattern.PatternQty}");
            sw.WriteLine($"REF={name}");
            sw.WriteLine($"R={0}");//暂时用无纹？
            sw.WriteLine($"BEW={part.PartCalc}");
            sw.WriteLine($"BEM={part.PartRemark}");
            sw.WriteLine($"BEM2={part.PartRemark2}");
            sw.WriteLine($"BEM3={part.PartRemark3}");
            //sw.WriteLine($"FIGREF={part.FigID}");
            //sw.WriteLine($"FIGPARTID={part.FigPartID}");
            //sw.WriteLine($"FIGDEF={part.FigDef}");
            //sw.WriteLine($"DRAWING={part.Drawing}");
            sw.WriteLine($"[ISTK-{index}]");
        }


    }
}
